/*
 * This is the main engine for the game
 * This engine handles all the drawing and will call
 * all of the update methods of the objects in the game
 * 
 * initialize runs once when the game runs
 */
package engine;

import gameElements.Map;
import gameElements.Player;
import gameElements.Room;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;




public class GameEngine extends GameBase {

	private Random seedGenerator;
	private Map basement;
	private Map mainFloor;
	private Map secondFloor;
	private Map[] mapArray;
	private boolean isServer;
	private int seed;	
	private ArrayList<Player> playerList;
	private Player playerClient;



	public static void main(String[] args) {
		new GameEngine();
	}

	public GameEngine(){

		GameFrame gameFrame = new GameFrame();		
		GameCanvas gameCanvas = new GameCanvas(this, 400, 300);		
		gameFrame.add(gameCanvas);
		initialize();
		gameCanvas.start();
		
		
		

	}
	

	public void initialize(){
		

		//ask user if server or client
		//server prints ip
		//client prompts for ip
		
		//refer to networking.txt in doc for more detailed info
		
		isServer = true; //for testing
		

		if(isServer){
			seedGenerator = new Random();
			seed = seedGenerator.nextInt();
		}else{
			//ask for seed from server
			//seed = sentFromServer;
		}

		//generate all floors
		basement = new Map(seed);
		basement.generateMap(0);
		mainFloor = new Map(seed);
		mainFloor.generateMap(1);
		secondFloor = new Map(seed);
		secondFloor.generateMap(2);

		mapArray = new Map[3];
		mapArray[0] = basement;
		mapArray[1] = mainFloor;
		mapArray[2] = secondFloor;

		mapArray[0].createDoors();
		mapArray[1].createDoors();
		mapArray[2].createDoors();

		//printRooms(0);
		//printRooms(1);
		//printRooms(2);
		//printNumDoors(1);
		printRoomsAndDoors(1);
		
		
		playerList = new ArrayList<Player>();
		playerClient = new Player(0);
		playerList.add(playerClient);
		playerClient.loadMap(mapArray);
		
		loadImages();
	}
	
	

	public void loadImages(){ //load the image files here
				
		//load room images
		for (int i = 0; i < mapArray.length; i++) {
			for (int j = 0; j < mapArray[i].getRoomArray().length; j++) {
				for (int k = 0; k < mapArray[i].getRoomArray().length; k++) {
					if(mapArray[i].getRoomArray()[j][k] != null)
						mapArray[i].getRoomArray()[j][k].loadRoomImages();
				}
			}
		}
		
		//load the client character spritesheet
		//use case structure based on playerID
		playerClient.loadImages("Will19by28_edited-1.png", 3, 3);
	}

	public void update(){ //call the update methods of the objects in the game
		
		
		
		
		
		System.exit(0); //for testing
	}

	public void draw(Graphics g){
		
		//draw the room that the player is currently in
		mapArray[playerClient.getCurrentRoom()[3]].getRoomArray()[playerClient.getCurrentRoom()[0]][playerClient.getCurrentRoom()[1]].draw(g);
		
	}

	public void printRooms(int floor){//for testing
		Room[][] roomArray = mapArray[floor].getRoomArray();				

		for (int i = 0; i < roomArray.length; i++) {
			for (int j = 0; j < roomArray.length; j++) {
				if(roomArray[i][j] == null)
					System.out.printf(" ");
				else{
					if(i == 20 && j == 20)
						System.out.printf("*");
					else
						System.out.printf("R");
				}
			}
			System.out.println();			
		}
	}

	public void printNumDoors(int floor){
		int[][] doorsArray = new int[mapArray[floor].getMaxRooms() * 2][mapArray[floor].getMaxRooms() * 2];
		Room[][] roomArray = mapArray[floor].getRoomArray();
		for (int i = 0; i < doorsArray.length; i++) {
			for (int j = 0; j < doorsArray.length; j++) {
				if(roomArray[i][j] != null)
					doorsArray[i][j] = roomArray[i][j].getNumDoors();
			}
		}

		//print
		for (int i = 0; i < doorsArray.length; i++) {
			for (int j = 0; j < doorsArray.length; j++) {
				if(doorsArray[i][j] == 0)
					System.out.printf(" ");
				else
					System.out.printf("%d", doorsArray[i][j]);
			}
			System.out.println();
		}
	}

	public void printRoomsAndDoors(int floor){
		Room[][] roomArray = mapArray[floor].getRoomArray();
		
		for (int i = 0; i < roomArray.length; i++) {
			for (int j = 0; j < roomArray.length; j++) {
				if(roomArray[i][j]== null){
					System.out.printf("      ");
				}
				else{
					if(i == 20 && j == 20)
						System.out.printf("{");
					else
						System.out.printf("[");

					for (int k = 0; k < 4; k++) {
						if(roomArray[i][j].getDoor(k))
							System.out.printf("T");
						else
							System.out.printf("F");
					}
					if(i == 20 && j == 20)
						System.out.printf("}");
					else
						System.out.printf("]");
				}
			}
			System.out.println();
		}


	}



}
